home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / contextlib.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  115 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import sys
  5. __all__ = [
  6.     'contextmanager',
  7.     'nested',
  8.     'closing']
  9.  
  10. class GeneratorContextManager(object):
  11.     
  12.     def __init__(self, gen):
  13.         self.gen = gen
  14.  
  15.     
  16.     def __enter__(self):
  17.         
  18.         try:
  19.             return self.gen.next()
  20.         except StopIteration:
  21.             raise RuntimeError("generator didn't yield")
  22.  
  23.  
  24.     
  25.     def __exit__(self, type, value, traceback):
  26.         if type is None:
  27.             
  28.             try:
  29.                 self.gen.next()
  30.             except StopIteration:
  31.                 return None
  32.  
  33.             raise RuntimeError("generator didn't stop")
  34.         elif value is None:
  35.             value = type()
  36.         
  37.         
  38.         try:
  39.             self.gen.throw(type, value, traceback)
  40.             raise RuntimeError("generator didn't stop after throw()")
  41.         except StopIteration:
  42.             exc = None
  43.             return exc is not value
  44.         except:
  45.             if sys.exc_info()[1] is not value:
  46.                 raise 
  47.             
  48.  
  49.  
  50.  
  51.  
  52. def contextmanager(func):
  53.     
  54.     def helper(*args, **kwds):
  55.         return GeneratorContextManager(func(*args, **kwds))
  56.  
  57.     
  58.     try:
  59.         helper.__name__ = func.__name__
  60.         helper.__doc__ = func.__doc__
  61.         helper.__dict__ = func.__dict__
  62.     except:
  63.         (None,)
  64.  
  65.     return helper
  66.  
  67.  
  68. def nested(*managers):
  69.     exits = []
  70.     vars = []
  71.     exc = (None, None, None)
  72.     
  73.     try:
  74.         for mgr in managers:
  75.             exit = mgr.__exit__
  76.             enter = mgr.__enter__
  77.             vars.append(enter())
  78.             exits.append(exit)
  79.         
  80.         yield vars
  81.     except:
  82.         exc = sys.exc_info()
  83.     finally:
  84.         while exits:
  85.             exit = exits.pop()
  86.             
  87.             try:
  88.                 if exit(*exc):
  89.                     exc = (None, None, None)
  90.             continue
  91.             exc = sys.exc_info()
  92.             continue
  93.  
  94.         if exc != (None, None, None):
  95.             raise exc[0], exc[1], exc[2]
  96.         
  97.  
  98.  
  99. nested = contextmanager(nested)
  100.  
  101. class closing(object):
  102.     
  103.     def __init__(self, thing):
  104.         self.thing = thing
  105.  
  106.     
  107.     def __enter__(self):
  108.         return self.thing
  109.  
  110.     
  111.     def __exit__(self, *exc_info):
  112.         self.thing.close()
  113.  
  114.  
  115.